package com.wty.love.smallGames;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.wty.love.R;

import java.util.HashSet;
import java.util.Random;

public class TicTacToeActivity extends AppCompatActivity implements View.OnClickListener {
    private String winStr = "哎哟不错哟，赢了#哇赢了，太厉害了呀#赢了哈哈哈哈，轻轻松松#我就知道你这么聪明肯定会赢#厉害厉害，赢了";
    private String transportStr = "哎呀，被人机吊打#输了，得加油呀#这是最低级的人机，怎么能输了#怎么输了呢，不应该#竟然输了，要不我把人机等级调第一点？";
    private View newGame;
    private TextView winNum;
    private TextView content;
    //棋子
    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private TextView textView4;
    private TextView textView5;
    private TextView textView6;
    private TextView textView7;
    private TextView textView8;
    private TextView textView9;

    private boolean canPlay = true;

    private Random random;

    //记录总局数
    private int count;
    //记录胜利几局
    private int win = 0;

    private TextView[] textViews;
    private int[] qiPan = new int[9];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tic_tac_toe);
        initView();
        initData();
        initClick();
    }

    private void initView() {
        newGame = findViewById(R.id.newGame);
        winNum = findViewById(R.id.winNum);
        content = findViewById(R.id.content);

        textView1 = findViewById(R.id.textView1);
        textView2 = findViewById(R.id.textView2);
        textView3 = findViewById(R.id.textView3);
        textView4 = findViewById(R.id.textView4);
        textView5 = findViewById(R.id.textView5);
        textView6 = findViewById(R.id.textView6);
        textView7 = findViewById(R.id.textView7);
        textView8 = findViewById(R.id.textView8);
        textView9 = findViewById(R.id.textView9);

        random = new Random();
        textViews = new TextView[9];

        textViews[0] = textView1;
        textViews[1] = textView2;
        textViews[2] = textView3;
        textViews[3] = textView4;
        textViews[4] = textView5;
        textViews[5] = textView6;
        textViews[6] = textView7;
        textViews[7] = textView8;
        textViews[8] = textView9;
    }

    private void initData() {
        Toast.makeText(this,"点击棋盘就是开始下棋",Toast.LENGTH_SHORT).show();
        clearQiPan();
    }

    private void initClick() {
        newGame.setOnClickListener(v->{
            clearQiPan();
        });
    }

    private void setTextViewText(TextView view,int num){
        if (num==1){//琪琪
            view.setText("x");
        }else if (num==2){//电脑
            view.setText("o");
        }
    }

    private void clearQiPan(){
        canPlay = true;

        content.setText("");

        textView1.setText(" ");
        textView2.setText(" ");
        textView3.setText(" ");
        textView4.setText(" ");
        textView5.setText(" ");
        textView6.setText(" ");
        textView7.setText(" ");
        textView8.setText(" ");
        textView9.setText(" ");

        for (int i = 0;i<qiPan.length;i++){
            qiPan[i] = 0;
        }
    }

    public void clickQiPan(View v){
        if (!canPlay) return;
        int n = -1;
        switch (v.getId()){
            case R.id.textView1:
                n=0;
                break;
            case R.id.textView2:
                n=1;
                break;
            case R.id.textView3:
                n=2;
                break;
            case R.id.textView4:
                n=3;
                break;
            case R.id.textView5:
                n=4;
                break;
            case R.id.textView6:
                n=5;
                break;
            case R.id.textView7:
                n=6;
                break;
            case R.id.textView8:
                n=7;
                break;
            case R.id.textView9:
                n=8;
                break;
        }
        if (qiPan[n]!=0) return;
        qiPan[n] = 1;
        setTextViewText(textViews[n],1);
        whoWin();
        if (!canPlay) return;

        //jiqiren
        int rNum = random.nextInt(9);
        while (true){
            if (qiPan[rNum]==0){
                break;
            }
            rNum = random.nextInt(9);
        }
        qiPan[rNum] = 2;
        setTextViewText(textViews[rNum],2);
        whoWin();

    }

    private boolean pD(){//true 满了
        int count = 0;
        for (int i:qiPan){
            if (i==0){
                count++;
            } else if (i==1){

            } else if (i==2){

            }
        }
        if (count==0){//满了
            return true;
        }
        return false;
    }

    /*
    1 2 3
    4 5 6
    7 8 9
     */
    public void whoWin(){
        System.out.println("win "+win);
        System.out.println("count "+count);
        HashSet<Integer> one = new HashSet<>();
        HashSet<Integer> two = new HashSet<>();
        for (int i=0;i<qiPan.length;i++){
            if (qiPan[i]==1){
                one.add(i+1);
            } else if (qiPan[i]==2){
                two.add(i+1);
            }
        }
        System.out.println("one "+one);
        System.out.println("two "+two);
        //判断
        if (
                one.contains(1)&&one.contains(2)&&one.contains(3)||
                one.contains(1)&&one.contains(4)&&one.contains(7)||
                one.contains(1)&&one.contains(5)&&one.contains(9)||
                one.contains(2)&&one.contains(5)&&one.contains(8)||
                one.contains(3)&&one.contains(5)&&one.contains(7)||
                one.contains(3)&&one.contains(6)&&one.contains(9)||
                one.contains(4)&&one.contains(5)&&one.contains(6)||
                one.contains(7)&&one.contains(8)&&one.contains(9)
        ){
            count++;
            canPlay = false;
            win++;
            content.setText("赢了");
            String ss[] = winStr.split("#");
            int c = random.nextInt(ss.length);
            Toast.makeText(this,ss[c],Toast.LENGTH_SHORT).show();
            if (count==0||win==0){
                winNum.setText("总场次："+count+" 胜率：0%");
            }else {
                winNum.setText("总场次："+count+" 胜率："+(int)(((win*1.0)/(count*1.0))*100)+"%");
            }
            return;
        }
        if (
                two.contains(1)&&two.contains(2)&&two.contains(3)||
                two.contains(1)&&two.contains(4)&&two.contains(7)||
                two.contains(1)&&two.contains(5)&&two.contains(9)||
                two.contains(2)&&two.contains(5)&&two.contains(8)||
                two.contains(3)&&two.contains(5)&&two.contains(7)||
                two.contains(3)&&two.contains(6)&&two.contains(9)||
                two.contains(4)&&two.contains(5)&&two.contains(6)||
                two.contains(7)&&two.contains(8)&&two.contains(9)
        ){
            count++;
            canPlay = false;
            content.setText("输了");
            String ss[] = transportStr.split("#");
            int c = random.nextInt(ss.length);
            Toast.makeText(this,ss[c],Toast.LENGTH_SHORT).show();
            if (count==0||win==0){
                winNum.setText("总场次："+count+" 胜率：0%");
            }else {
                winNum.setText("总场次："+count+" 胜率："+(int)(((win*1.0)/(count*1.0))*100)+"%");
            }
            return;
        }

        if (pD()){
            count++;
            canPlay = false;
            content.setText("平局");
            Toast.makeText(this,"不相上下",Toast.LENGTH_SHORT).show();
            if (count==0||win==0){
                winNum.setText("总场次："+count+" 胜率：0%");
            }else {
                winNum.setText("总场次："+count+" 胜率："+(int)(((win*1.0)/(count*1.0))*100)+"%");
            }
        }
    }

    @Override
    public void onClick(View v) {

    }
}